/*
 * Copyright 2016-2021 the original author.All rights reserved.
 * Kingstar(aiteasoft@163.com)
 * The license,see the LICENSE file.
 */

package com.aiteasoft.school.scores.rest;

import static com.automvc.common.service.CommService.hasNull;
import static com.automvc.common.service.CommService.isNullPara;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.teasoft.bee.osql.service.ObjSQLRichService;
import org.teasoft.beex.util.CnNum;

import com.aiteasoft.school.scores.entity.Scores;
import com.aiteasoft.school.scores.service.ScoresForTeacherService;
import com.automvc.common.jq.PageWarp;
import com.automvc.common.jq.Result;

/**
 * @author AiTeaSoft.com
 * @since  1.9
 * Create on 2021-01-18 18:29:21
 */
@RestController("scoresForTeacher")
@RequestMapping("scoresForTeacher")
public class ScoresForTeacherRest {  
	@Autowired
	ObjSQLRichService objSQLRichService;

	@Autowired
	ScoresForTeacherService scoresForTeacherService;

	@RequestMapping("/list")
	public Result list(Scores scores, 
			@RequestParam(value = "page", defaultValue = "1", required = false) int page,
			@RequestParam(value = "rows", defaultValue = "20", required = false) int rows) {

//		try {
//			Class.forName("org.teasoft.beex.util.CnNum");
//		} catch (Exception e) {
//			System.err.println(" load the CnNum");
//           e.printStackTrace();
//		}
//		System.err.println(CnNum.tranToUpper(123.143D));
		
		Result result = new Result();
		if (isNullPara(result, scores)) return result;
		
		scores = scoresForTeacherService.process(scores);
		
		PageWarp pageWarp = scoresForTeacherService.countAndSelect(scores, (page - 1) * rows, rows);
		result.setTotal(pageWarp.getTotal());
		result.setRows(pageWarp.getList());

		return result;
	}

	@RequestMapping("/edit")
	public Result update(Scores scores) {

		Result result = new Result();
		if (isNullPara(result, scores)) return result;
		if(scoresForTeacherService.isWrongBizForUpdate(result, scores)) return result;

		String checkFields = "classno,term,examno,subjectno,subject,stuno,name,teacherno,teachername,creater,status,";
		if (hasNull(result, scores, checkFields)) return result;
		
		scores = scoresForTeacherService.process(scores);
		
		int num = scoresForTeacherService.update(scores);
		result.setTotal(num);
		if (num <= 0) result.setErrorMsg("update failed!");
		return result;
	}
	
	@RequestMapping("/upload")
	public Result upload(@RequestParam("file") MultipartFile file) {
	
		Result result = new Result();
		
		//TODO判断是否有权限.
		
        String fileName = file.getOriginalFilename();
        try {
        	int total=scoresForTeacherService.uploadScore(file.getInputStream());
        	result.setMsg("update rows:" +total);
		} catch (Exception e) {
			result.setErrorCode("10500");
			result.setErrorMsg("upload failed!");
		}
		
//		System.out.println("--------------------"+fileName);
		
		return result;
	}
	
	/*	@RequestMapping("/upload3")
		public String upload3(@RequestParam("file") MultipartFile file, HttpServletRequest req)
				throws IllegalStateException, IOException {
	
			// 判断文件是否为空，空则返回失败页面
			if (file.isEmpty()) {
				return "failed";
			}
			// 获取文件存储路径（绝对路径）
			String path = req.getServletContext().getRealPath("/WEB-INF/file");
			// 获取原文件名
			String fileName = file.getOriginalFilename();
			
			System.out.println("--------------------"+fileName);
			// 创建文件实例
			File filePath = new File(path, fileName);
			// 如果文件目录不存在，创建目录
			if (!filePath.getParentFile().exists()) {
				filePath.getParentFile().mkdirs();
				System.out.println("创建目录" + filePath);
			}
			// 写入文件
			file.transferTo(filePath);
			return "ok";
		}  */

}